package com.zm.tools.util.multidatasource;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * @author zoum
 * @create 2018/6/15 15:22
 */
@Aspect
public class DataSourceAspect {

    public void before(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        if (isSlave(methodName)){
            DynamicDataSourceHolder.markSlave();
        }else {
            DynamicDataSourceHolder.markMaster();
        }
    }

    private boolean isSlave(String methodName){
        return StringUtils.startsWithAny(methodName, "get","query","select","find");
    }
}
